//
//  CustomLayout.m
//  CollectionTest
//
//  Created by A on 2020/11/7.
//  Copyright © 2020 TyTeam. All rights reserved.
//

#import "CustomLayout.h"

//列数
static int columnCount = 2;
//行间距
static int rowMargin = 10;
//列间距
static int columnMargin = 10;
//item边距
static UIEdgeInsets edgeInsets = {10,10,10,10};
//

@interface CustomLayout ()
//总高度
@property (nonatomic, assign) CGFloat contentHeight;
//所有cell 布局信息
@property (nonatomic, strong) NSMutableArray *attributesArr;
//每一列当前高度
@property (nonatomic, strong) NSMutableArray *columnHeight;
//item宽度
@property (nonatomic, assign) CGFloat itemWidth;
@end

@implementation CustomLayout

/**
 初始化时调用一次
 collection 布局发生改变时会调用
 */
- (void)prepareLayout {
    
    [super prepareLayout];
    
    self.contentHeight = 0;
    
    //重置每列高度
    [self.columnHeight removeAllObjects];
    for (int i = 0; i < columnCount; i++) {
        [self.columnHeight addObject:@(edgeInsets.top)];
    }
    
    //重置cell布局信息
    NSInteger cellCount = [self.collectionView numberOfItemsInSection:0];
    
    [self.attributesArr removeAllObjects];
    for (NSInteger i = 0; i< cellCount; i ++) {
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        
        UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
        
        [self.attributesArr addObject:attributes];
    }
    
}


- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    CGFloat item_height = [self.delegate getCellHeightForItemAtIndex:indexPath.item itemWidth:self.itemWidth];
    
    
    //找最矮那一列
    int shortestColumn = 0;
    
    CGFloat minHeight = [self.columnHeight[0] doubleValue];
    
    for (int i = 1; i < columnCount; i++) {
        CGFloat height = [self.columnHeight[i] doubleValue];
        
        if (minHeight > height) {
            minHeight = height;
            shortestColumn = i;
        }
    }
    
    //确定frame值
    CGFloat item_x = edgeInsets.left + shortestColumn * (self.itemWidth + columnMargin);
    CGFloat item_y = minHeight;
    if (item_y != edgeInsets.top) {
        item_y = item_y + rowMargin;
    }
    
    attributes.frame = CGRectMake(item_x, item_y, self.itemWidth, item_height);
    
    //更新数据
    CGFloat height = CGRectGetMaxY(attributes.frame);
    self.columnHeight[shortestColumn] = @(height);
    
    if (self.contentHeight < height) {
        self.contentHeight = height;
    }
    
    return attributes;
}


- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    return self.attributesArr;
}



- (CGSize)collectionViewContentSize {
    
    return CGSizeMake(0, self.contentHeight + edgeInsets.bottom);
}


#pragma mark === getter
- (NSMutableArray *)columnHeight
{
    if (!_columnHeight) {
        _columnHeight = [NSMutableArray array];
    }
    return _columnHeight;
}

- (NSMutableArray *)attributesArr
{
    if (!_attributesArr) {
        _attributesArr = [NSMutableArray array];
    }
    return _attributesArr;
}

- (CGFloat)itemWidth {
    
    CGFloat collectionView_wight = self.collectionView.frame.size.width;
    
    CGFloat width = (collectionView_wight - edgeInsets.left - edgeInsets.right - (columnCount - 1) * columnMargin)/columnCount;
    
    return width;
    
}
@end
